package com.example.service.impl;

import com.example.mapper.UserMapper;
import com.example.mapper.WorkMapper;
import com.example.pojo.Comment;
import com.example.pojo.User;
import com.example.pojo.Work;
import com.example.service.UserService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WorkMapper workMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getUsers();
    }

    @Override
    public void register(User user) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findUserByUsername(user.getUsername());
        if (existingUser != null) {
            throw new RuntimeException("用户名已存在！");
        }
        // 此处应加密用户密码
        userMapper.insertUser(user);
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.findUserByUsername(username);
        if (user == null || !user.getPassword().equals(password)) {
            throw new RuntimeException("用户名或密码错误！");
        }
        return user;

    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
    @Override
    public User getUserById(Integer userId) {
        return userMapper.findUserById(userId);
    }


    @Override
    public List<Work> getUserWorksByUserId(Integer userId) {
        return workMapper.findByUserId(userId);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteUserById(userId);
    }
}
